package com.ls.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ls.config.satoken.SaTokenUtilPy;
import com.ls.controller.company.model.AddCompanyModel;
import com.ls.controller.company.model.EditCompanyModel;
import com.ls.controller.company.model.IsSetVo;
import com.ls.controller.template.model.AddTemplateModel;
import com.ls.controller.template.model.EditTemplateModel;
import com.ls.controller.template.model.QueryTemplateModel;
import com.ls.entity.*;
import com.ls.enums.RoleEnum;
import com.ls.enums.TemplateStatusEnum;
import com.ls.exception.SysException;
import com.ls.mapper.BannerMapper;
import com.ls.mapper.CompanyMapper;
import com.ls.service.IBannerService;
import com.ls.service.ICompanyService;
import com.ls.utils.ResultData;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static com.ls.constant.SystemConstants.*;

@Service
@Slf4j
@RequiredArgsConstructor
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements ICompanyService {
    /**
     * APP端 新增企业
     */
    @Override
    public ResultData add(AddCompanyModel model) {
        Customer customer = SaTokenUtilPy.getCustomer();
        Company company = new Company();
        BeanUtils.copyProperties(model, company);
        company.setCustomerId(customer.getCustomerId());
        company.setCreatorId(customer.getCustomerId());
        company.setUpdaterId(customer.getCustomerId());
        save(company);
        return ResultData.success();
    }

    /**
     * 更新企业
     */
    @Override
    public ResultData update(EditCompanyModel model) {
        Customer customer = SaTokenUtilPy.getCustomer();
        Company company = getById(model.getCompanyId());
        if (company == null){
            return ResultData.failure("企业id不存在");
        }
        BeanUtils.copyProperties(model, company);
        company.setUpdaterId(customer.getCustomerId());
        company.setUpdateTime(LocalDateTime.now());
        saveOrUpdate(company);
        return ResultData.success();
    }
    /**
     * 查看企业
     */
    @Override
    public ResultData getCompany(Long customerId) {
        if (customerId == null){
            Customer customer = SaTokenUtilPy.getCustomer();
            customerId = customer.getCustomerId();
        }
        //根据用户id查询企业
        Company company = this.baseMapper.selectOne(new LambdaQueryWrapper<Company>()
                .eq(Company::getCustomerId, customerId));
        return ResultData.successData(company);
    }
    /**判断当前用户是否设置企业信息*/
    @Override
    public ResultData isSetCompany() {
        Customer customer = SaTokenUtilPy.getCustomer();
        Company company = getOne(new LambdaQueryWrapper<Company>().eq(Company::getCustomerId, customer.getCustomerId()));
        if (company == null||company != null&&company.getCompanyName()==null){
            return ResultData.successData(0);
        }else {
            return ResultData.successData(1);
        }
    }
    /**ai生成短信要素判断*/
    @Override
    public ResultData isSet() {
        Customer customer = SaTokenUtilPy.getCustomer();
        Company company = getOne(new LambdaQueryWrapper<Company>()
                .eq(Company::getCustomerId, customer.getCustomerId())
                .orderByAsc(Company::getCreateTime)
                .last("LIMIT 1")
        );
        IsSetVo isSetVo=new IsSetVo();
        if (StringUtils.isNotEmpty(company.getCompanyName())){
            isSetVo.setIsName(1);
        }else {
            isSetVo.setIsName(0);
        }
        if (StringUtils.isNotEmpty(company.getPhone())){
            isSetVo.setIsPhone(1);
        }else {
            isSetVo.setIsPhone(0);
        }
        if (StringUtils.isNotEmpty(company.getProvince())&&StringUtils.isNotEmpty(company.getCity())&&StringUtils.isNotEmpty(company.getDistrict())&&StringUtils.isNotEmpty(company.getAddress())){
            isSetVo.setIsAddress(1);
        }else {
            isSetVo.setIsAddress(0);
        }
        return ResultData.successData(isSetVo);
    }
}
